﻿using FPSO.HMI.Controls;
using SafeLoad.NControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FPSO.HMI.Pipe
{
    class PipeLineElement : IPipeElement
    {
        public PipeLineElement(NPipeLine pipeLine) : base()
        {
            PipeLine = pipeLine;
            Port1 = null;
            Port2 = null;
            Pump = null;
            Valve = null;

            if (pipeLine.RenderTransform is RotateTransform rotate)
            {
                //竖向
                Port1Pos = new Point(Canvas.GetLeft(pipeLine) - pipeLine.ActualHeight / 2,
                    Canvas.GetTop(pipeLine));
                Port2Pos = new Point(Canvas.GetLeft(pipeLine) - pipeLine.ActualHeight / 2,
                    Canvas.GetTop(pipeLine) + pipeLine.ActualWidth);
            }
            else
            {
                Port1Pos = new Point(Canvas.GetLeft(pipeLine), 
                    Canvas.GetTop(pipeLine) + pipeLine.ActualHeight / 2);
                Port2Pos = new Point(Canvas.GetLeft(pipeLine) + pipeLine.ActualWidth,
                    Canvas.GetTop(pipeLine) + pipeLine.ActualHeight / 2);
            }

            LineRect = new Rect(new Point(Port1Pos.X - pipeLine.ActualHeight / 2, Port1Pos.Y - pipeLine.ActualHeight / 2),
                new Point(Port2Pos.X + pipeLine.ActualHeight / 2, Port2Pos.Y + pipeLine.ActualHeight / 2));
        }
        public IPipeElement Port1 { get; set; }
        public IPipeElement Port2 { get; set; }

        public NPipeLine PipeLine { get; private set; }

        public NPump Pump { get; set; }
        public UCValve Valve { get; set; }

        public Point Port1Pos { get; private set; }
        public Point Port2Pos { get; private set; }
        private Rect LineRect { get; set; }

        public PortState Port1State { get; private set; }
        public PortState Port2State { get; private set; }

        public bool PointInLine(Point p)
        {
            return LineRect.Contains(p);
        }

        public void Connect(PipeLineElement pipeLine)
        {
            if (Port1 == null)
            {
                if (pipeLine.Port1 == null && PointDistance.Distance(Port1Pos, pipeLine.Port1Pos) < 10)
                {
                    pipeLine.Port1 = this;
                    Port1 = pipeLine;
                }
                else if (pipeLine.Port2 == null && PointDistance.Distance(Port1Pos, pipeLine.Port2Pos) < 10)
                {
                    pipeLine.Port2 = this;
                    Port1 = pipeLine;
                }
            }
            if (Port2 == null)
            {
                if (pipeLine.Port1 == null && PointDistance.Distance(Port2Pos, pipeLine.Port1Pos) < 10)
                {
                    pipeLine.Port1 = this;
                    Port2 = pipeLine;
                }
                else if (pipeLine.Port2 == null && PointDistance.Distance(Port2Pos, pipeLine.Port2Pos) < 10)
                {
                    pipeLine.Port2 = this;
                    Port2 = pipeLine;
                }
            }
        }

        public bool Port1IsPumpIn()
        {
            if (Pump != null)
            {
                if (Math.Abs(Port1Pos.X - Port2Pos.X) < 0.1)
                {
                    //竖向
                    if (Pump.RenderTransform is RotateTransform rotate)
                    {
                        if (rotate.Angle == 90)
                        {
                            
                        }
                        else if (rotate.Angle == -90)
                        {

                        }
                    }
                }
                else
                {
                    //横向
                    if (Pump.RenderTransform is RotateTransform rotate)
                    {
                        return true;
                    }
                }
            }

            return false;
        }


        public override void SetInPort(IPipeElement element)
        {
            if (Port1 == element)
            {
                Port1State = PortState.In;
            }
            else if (Port2 == element)
            {
                Port2State = PortState.In;
            }
        }

        public override void SetOutPort(IPipeElement element)
        {
            if (Port1 == element)
            {
                Port1State = PortState.Out;
            }
            else if (Port2 == element)
            {
                Port2State = PortState.Out;
            }
        }

        public override void UpdateUI()
        {
            if (PipeLine != null)
            {
                bool active = Port1State != PortState.Stop;
                double moveSpeed = Port1State == PortState.In ? 0.5 : -0.5;

				if (PipeLine.PipeLineActive != active)
				{
					PipeLine.PipeLineActive = active;
				}
				if (Math.Abs(PipeLine.MoveSpeed - moveSpeed) > 0.1)
				{
					PipeLine.MoveSpeed = moveSpeed;
				}
			}
		}

        public override void ResetState()
        {
            Port1State = PortState.Stop;
            Port2State = PortState.Stop;
        }
    }
}
